package com.example.springdemo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        UserController userController=new UserController();
        UserService userService=new UserService();
        System.out.println(userService);
// 利用反射来实现
        Class<? extends UserController> clazz = userController.getClass();
        // 获取其中的属性值
        Field serviceField = clazz.getDeclaredField("userService");

        String name = serviceField.getName();
        String methodName = "set"+name.substring(0,1).toUpperCase()+name.substring(1,name.length());
        // 获取到方法的对象
        Method method = clazz.getMethod(methodName,UserService.class);
        // 执行方法
        method.invoke(userController,userService);
        System.out.println(userController.getUserService());
    }
}
